import Vue from 'vue';
import App from './App';
import router from './router';
import axios from 'axios';
import ElementUI from 'element-ui';
import VueResource from 'vue-resource'
import 'element-ui/lib/theme-default/index.css';    // 默认主题
// import '../static/css/theme-green/index.css';       // 浅绿色主题
import store from './store.js'
import "babel-polyfill";
import $ from 'jquery';
var VueCookie = require('vue-cookie');
Vue.use(VueCookie);
Vue.use(ElementUI);
Vue.use(VueResource);
Vue.prototype.$axios = axios;
Vue.prototype.throw_notice = function (notice){
	this.$alert(notice, '复旦儿童医院挂号系统');
}
new Vue({
	store,
    router,
    render: h => h(App)
}).$mount('#app');
router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
        if (VueCookie.get('username') == null ) {  // 通过vuex state获取当前的token是否存在
            next({name:'login'});
        }else if(VueCookie.get('token') == null ){
        	next({name:'login'});
        }else {
            next();
        }
    }else {
        next();
    }
})
Vue.http.interceptors.push((request, next) => {
	if(VueCookie.get('token') != null){
		request.headers.set('Authorization', 'Bearer '+VueCookie.get('token'));
	}
  	next(response => {
    	return response
  	})
})